package com.example.likegifmarker.controller.fragments;

import android.content.Intent;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.likegifmarker.Model.Beans.ChangeEvent;
import com.example.likegifmarker.R;
import com.example.likegifmarker.Utils.CacheUtil;
import com.example.likegifmarker.Utils.PermissionHelper;
import com.example.likegifmarker.controller.activities.MainActivity;
import com.example.likegifmarker.view.wdigets.FocusTextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/*
 * Created by　Dullyoung on 2020/12/16
 */
public class IndexDetailFragment extends BaseFragment {
    int idx;
    @BindView(R.id.tv_music_name)
    FocusTextView mTvMusicName;
    @BindView(R.id.tv_desp)
    TextView mTvDesp;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;

    public IndexDetailFragment(int idx) {
        this.idx = idx;
    }

    @BindView(R.id.tv_share)
    TextView mTvShare;
    @BindView(R.id.iv_share)
    ImageView mIvShare;
    @BindView(R.id.tv_comment)
    TextView mTvComment;
    @BindView(R.id.iv_comment)
    ImageView mIvComment;
    @BindView(R.id.tv_like)
    TextView mTvLike;
    @BindView(R.id.iv_like)
    ImageView mIvLike;
    @BindView(R.id.cl_icon)
    ConstraintLayout mClIcon;
    @BindView(R.id.cl_root)
    ConstraintLayout mClRoot;
    @BindView(R.id.vv_index)
    VideoView mVvIndex;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index_detail;
    }

    @Override
    protected void initViews() {
        if (idx == 2) {
            setVideo();
        }
        mTvUserName.setText("@卢本伟牛逼666");
        mTvUserName.getPaint().setFakeBoldText(true);
    }

    @OnClick(R.id.cl_root)
    public void onViewClicked() {
        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity == null) return;
        mainActivity.permissionHelper.checkAndRequestPermission(mainActivity, new PermissionHelper.OnRequestPermissionsCallback() {
            @Override
            public void onRequestPermissionSuccess() {
                getVideo();
            }

            @Override
            public void onRequestPermissionError() {
                Toast.makeText(mainActivity, "必须给权限", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getVideo() {
        Intent pic1 = new Intent(Intent.ACTION_GET_CONTENT);
        pic1.setType("video/*");
        pic1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(pic1, 66);
    }

    private String videoPath = "";

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 66 && resultCode == RESULT_OK && null != data) {
            String fileName = getContext().getCacheDir().getPath() + "/index/index.mp4";

            if (CacheUtil.copyFileToPath(getContext(), data.getData(), fileName) == null) {
                return;
            }
            CacheUtil.setIndexVideoPath(fileName);
            setVideo();
        }
    }

    private void setVideo() {
        if (TextUtils.isEmpty(CacheUtil.getIndexVideoPath())) return;
        mVvIndex.setVideoPath(CacheUtil.getIndexVideoPath());
        mVvIndex.setOnErrorListener((mediaPlayer, i, i1) -> {
            mediaPlayer.reset();
            return true;
        });
        mVvIndex.setOnPreparedListener(MediaPlayer::start);
        mVvIndex.setOnCompletionListener(MediaPlayer::start);
    }

    @Override
    public void onResume() {
        super.onResume();
        mVvIndex.start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChange(ChangeEvent changeEvent) {
        if (mVvIndex != null && mVvIndex.isPlaying()) {
            mVvIndex.pause();
        }
        if (changeEvent.getIdx() == 2 && mVvIndex != null) {
            mVvIndex.start();
        }
    }
}
